package org.ucn.dmaa0914.temadesign.group2.modelLayer;
import java.util.HashSet;
import java.util.Iterator;

/**
 * The dvd container class is responsible of containing all dvds and perform lookups.
 * 
 * @author Kim Johannsen
 * @version 04-11-2014
 */
public class DvdContainer
{
    HashSet<Dvd> dvdCollection;
    
    // Provide a singleton instance, as this class is only ment to be instantiated once per program.
    public static final DvdContainer instance = new DvdContainer();
    
    /**
     * Constructor
     */
    public DvdContainer()
    {
        dvdCollection = new HashSet<Dvd>();
    }
    
    /*
     * Adds a dvd to the container, if it doesnt already exist
     * in the database.
     * 
     * @param p The dvd to add
     * @return True if the dvd was added. False if the dvd was already added.
     */
    public boolean addDvd(Dvd p)
    {
        return dvdCollection.add(p);
    }
    
    /**
     * Checks whether this container has a dvd.
     * 
     * @param p The dvd to check for
     * @return True if this container has this dvd. False if not.
     */
    public boolean containsDvd(Dvd p)
    {
        return dvdCollection.contains(p);
    }
    
    /**
     * Looks up a dvd by it's id.
     * 
     * @param id The id of the dvd to find.
     * @return The dvd with the specified id, or null if the dvd is not found.
     */
    public Dvd findDvd(int id)
    {
        Iterator<Dvd> i = dvdCollection.iterator();
        
        while( i.hasNext() )
        {
            Dvd p = i.next();
            
            if( p.getId() == id )
            {
                return p;
            }
        }
        
        return null;
    }
    
    /**
     * Looks up a dvd by it's barcode.
     * 
     * @param phone The barcode of the dvd to look for
     * @return The dvd with the specified barcode, or null if the dvd is not found.
     */
    public Dvd getDvd(int barcode)
    {
        Iterator<Dvd> i = dvdCollection.iterator();
        
        while( i.hasNext() )
        {
            Dvd p = i.next();
            
            if( p.getBarcode() == barcode )
            {
                return p;
            }
        }
        
        return null;
    }
    
    /**
     * Deletes a dvd from the container if present
     * 
     * @param p The dvd to delete
     */
    public void deleteDvd(Dvd p)
    {
        dvdCollection.remove(p);
    }
    
    /**
     * Retrieves all DVDs in this container
     */
    public HashSet<Dvd> getAll()
    {
        return dvdCollection;
    }
   
}
